import { store } from "@/store"
import { defineStore } from 'pinia'
import { ref, reactive } from 'vue'
import { showError } from '@/utils/msg-show'
import { getPageApi } from '@/api/user/avatar'

export const useUserAvatarStore = defineStore("UserAvatar", () => {

	const isZero = ref(true)
	const dataList = ref<any[]>([])
	const query = reactive<any>({
		pageNum: 1,
		pageSize: 30,
		totalSize: 0,
	})
    
    /** 分页获取 */
	async function getPage() {
		uni.showLoading({mask: true})
		await getPageApi(query).then( (res: any) => {
			query.totalSize = res.data.total
			dataList.value = res.data.list
		}).catch(err => {
			showError(err.msg || err.errMsg || err)
		}).finally(()=>{
           uni.hideLoading()
		})
	}

	/** 初始化 */
	async function init() {
		if (isZero.value) {
			await getPage()
			isZero.value = false
		}
	}

	return {
		init, getPage,
		query, dataList
	}

})

export function useUserAvatarStoreHook() {
	return useUserAvatarStore(store);
}